using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.Core.Domain.PageContents;
using System.Threading;
using ITS.DAL.DynamicContent;

namespace ITS.DAL.Concrete.EF.DynamicContent
{
    public class EFPageContentRepository : IPageContentRepository
    {
        private readonly EnterpriseContext context = new EnterpriseContext();

        public PageContentDescription GetPageContentDescriptionByNameInCurrentCulture(string strName)
        {
            string strCurrentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            return context.PageContentDescriptions.Single(desc => desc.PageContent.Name == strName && desc.LanguageID == strCurrentCulture);
        }

        public PageContentDescription GetPageContentDescriptionByID(int iID)
        {
            return context.PageContentDescriptions.Single(pc => pc.DescriptionID == iID);
        }

        public PageContentDescription GetPageContentDescription(string strName, string culture)
        {
            return context.PageContentDescriptions.Single(pc => pc.LanguageID == culture && pc.PageContent.Name == strName);
        }
        public PageContentDescription GetPageContentDescription(int PageContentID, string culture)
        {
            return context.PageContentDescriptions.Single(pc => pc.LanguageID == culture && pc.PageContent.PageContentID == PageContentID);
        }

        public IQueryable<PageContentDescription> GetAllDescriptions()
        {
            return context.PageContentDescriptions;
        }


        public IQueryable<PageContentDescription> GetDescriptionsByCategoryIdInCurrentCulture(int CategoryID)
        {
            string strCurrentCulture = Thread.CurrentThread.CurrentCulture.ToString();
            return context.PageContentDescriptions.Where(x => x.LanguageID == strCurrentCulture).Where(x => x.PageContent.CategoryID == CategoryID && x.PageContent.Active == true);
        }


        public PageContentDescription GetDescriptionByID(int DescriptionID)
        {
            return context.PageContentDescriptions.Single(x => x.DescriptionID == DescriptionID);
        }


        public void Save()
        {
            context.SaveChanges();
        }


        public IList<PageContent> GetAllPageContents()
        {
            return context.PageContents.ToList() ;
        }


        public PageContent GetPageContentByID(int PageContentID)
        {
            return context.PageContents.Single(x => x.PageContentID == PageContentID);
        }


        public IList<PageContentDescription> GetDescriptionsByPageContentID(int PageContentID)
        {
            return context.PageContentDescriptions.Where(x => x.PageContentID == PageContentID).ToList();
        }


        public IList<PageContentCategory> GetAllPageContentCategories()
        {
            return context.PageContentCategories.Where(x => x.Active == true).ToList();
        }


        public IList<PageContent> GetPageContentsByCategoryID(int CategoryID)
        {
            return context.PageContents.Where(x => x.CategoryID == CategoryID).ToList();
        }


        public IList<PageContentImage> GetPageContentImagesByPageContentID(int PageContentID)
        {
            return context.PageContentImages.Where(x => x.PageContentID == PageContentID).ToList();
        }


        public void AddPageContentImage(PageContentImage image, int PageContentID)
        {
            PageContent content = GetPageContentByID(PageContentID);
            content.PageContentImages.Add(image);
            Save();
        }


        public PageContentImage GetPageContentImageByID(Guid ImageID)
        {
            return context.PageContentImages.SingleOrDefault(x => x.ImageID == ImageID);
        }


        public void DelelePageContentImage(Guid ImageID)
        {
            PageContentImage image = GetPageContentImageByID(ImageID);
            context.PageContentImages.Remove(image);
            Save();
        }
    }
}
